/**
 * Created by Administrator on 2017/4/25 0025.
 */
var container;
var camera, scene, renderer;
var beta=0;
var gamma=0;
var controls;
var kale;
var bool_render=false;
var bool_loading=true;
var gammaChange=0;
var betaChange=0;
var mesh1;
var mesh2;
var mesh;
function threeInit()
{
    container = document.getElementById( 'div_3d' );

    camera = new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 1, 3000 );
    camera.aspect =window.innerWidth / window.innerHeight;
    camera.position.z = 50;


    scene = new THREE.Scene();

    var ambientLight = new THREE.AmbientLight(0xffffff, 1);
    scene.add(ambientLight);

    // var cylinderG=new THREE.CylinderGeometry(2000*0.95,113600*0.95,130*0.95,32,1,true);
    var cylinderG=new THREE.CylinderGeometry(8938/Math.PI/2,8938/Math.PI/2,1183,30,30,true);
    var cylinderM=new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('assets/images/1.png'),transparent:true,side:THREE.DoubleSide});
    mesh=new THREE.Mesh(cylinderG,cylinderM);
    mesh.scale.x=-1;
    mesh.material.opacity=1;
    mesh.renderOrder=1;
    scene.add(mesh);
    // mesh.rotation.y=230*Math.PI/180;

    var cylinderG1=new THREE.CylinderGeometry(8938/Math.PI/2,8938/Math.PI/2,0.99*1183,30,30,true);
    var cylinderM1=new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('assets/images/2.png'),transparent:true,side:THREE.DoubleSide});
    mesh1=new THREE.Mesh(cylinderG1,cylinderM1);
    mesh1.scale.x=-1;
    mesh1.scale.set(-0.995,0.995,0.995)
    mesh1.material.opacity=1;
    mesh1.renderOrder=2;
    scene.add(mesh1);
    // mesh1.rotation.y=240*Math.PI/180;

    var cylinderG2=new THREE.CylinderGeometry(8938/Math.PI/2,8938/Math.PI/2,1183,30,30,true);
    var cylinderM2=new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('assets/images/3.png'),transparent:true,side:THREE.DoubleSide});
    mesh2=new THREE.Mesh(cylinderG2,cylinderM2);
    mesh2.scale.x=-1;
    mesh2.material.opacity=1;
    mesh2.scale.set(-0.98,0.98,0.98)
    mesh2.renderOrder=3;
    scene.add(mesh2);
    // mesh2.rotation.y=240*Math.PI/180;

    renderer = new THREE.WebGLRenderer();
    renderer.setPixelRatio( window.devicePixelRatio );
    renderer.setClearColor(0x0,1);
    container.appendChild( renderer.domElement );
    onWindowResize();
    window.addEventListener( 'resize', onWindowResize, false );
    // In();
}
function threeIn()
{
    camera.zoom=0.0;
    TweenMax.to(camera,3,{ease:Linear.easeNone,zoom:1.3,onUpdate:function () {
        camera.updateProjectionMatrix();
    }});

    mesh.material.opacity=0;
    mesh.rotation.y=-150*Math.PI/180;
    TweenMax.to(mesh.material,3,{opacity:1,ease:Linear.easeNone});
    TweenMax.to(mesh.rotation,3,{y:195*Math.PI/180,ease:Cubic.easeInOut});
    // mesh.scale.x=0;
    // mesh.scale.y=0;
    // mesh.scale.z=0;
    // TweenMax.to(mesh.scale,3,{x:-1,y:1,z:1,ease:Linear.easeNone});

    mesh1.material.opacity=0;
    mesh1.rotation.y=-150*Math.PI/180;
    TweenMax.to(mesh1.material,3,{delay:.2,opacity:1,ease:Linear.easeNone});
    TweenMax.to(mesh1.rotation,3,{delay:.2,y:195*Math.PI/180,ease:Cubic.easeInOut,onComplete:function () {
        TweenMax.to(mesh1.rotation,5,{yoyo:true,repeat:-1,y:200*Math.PI/180,ease:Linear.easeNone});
    }});
    // mesh1.scale.x=0;
    // mesh1.scale.y=0;
    // mesh1.scale.z=0;
    // TweenMax.to(mesh1.scale,3,{x:-1,y:1,z:1,ease:Linear.easeNone});

    mesh2.material.opacity=0;
    mesh2.rotation.y=-150*Math.PI/180;
    TweenMax.to(mesh2.material,3,{delay:.5,opacity:1,ease:Linear.easeNone});
    TweenMax.to(mesh2.rotation,3,{delay:.5,y:195*Math.PI/180,ease:Cubic.easeInOut,onComplete:function () {
        TweenMax.to(mesh2.position,1,{yoyo:true,repeat:-1,z:5,x:5,ease:Cubic.easeInOut});
        TweenMax.to(mesh2.rotation,5,{yoyo:true,repeat:-1,y:200*Math.PI/180,ease:Linear.easeNone});
    }});



    setTimeout(function () {
        document.getElementById('div_3d').addEventListener('touchstart',function () {
            dollyIn();
        });
        document.getElementById('div_3d').addEventListener('mousedown',function () {
            dollyIn();
        });
        document.getElementById('div_3d').addEventListener('touchend',function () {
            dollyOut();
        });
        document.getElementById('div_3d').addEventListener('mouseup',function () {
            dollyOut();
        });


        window.addEventListener('deviceorientation', function(e){
            if(Math.abs(e.beta-beta)>0.3)
            {
                betaChange=e.beta-beta;
                beta=e.beta;
            }
            if(Math.abs(e.gamma-gamma)>0.3)
            {
                gammaChange=e.gamma-gamma;
                gamma=e.gamma;
            }

        });
    },4000);

    bool_render=true;

    controls = new THREE.OrbitControls( camera, renderer.domElement );
    controls.minDistance = 0;
    controls.maxDistance = 6000;
    controls.minPolarAngle=88*Math.PI/180;
    controls.maxPolarAngle=92*Math.PI/180;
    controls.enableDamping = true;
    controls.dampingFactor = 0.05;
    controls.rotateSpeed = -0.02;

    animate();
}

function dollyIn()
{
    TweenMax.killTweensOf(this);
    TweenMax.to(camera,0.2,{ease:Back.easeOut,zoom:1.2,onUpdate:function () {
        camera.updateProjectionMatrix();
    }});
}
function dollyOut()
{
    TweenMax.killTweensOf(this);
    TweenMax.to(camera,0.2,{ease:Back.easeOut,zoom:1.3,onUpdate:function () {
        camera.updateProjectionMatrix();
    }});
}

function onWindowResize( event ) {

    camera.aspect =window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize( window.innerWidth, window.innerHeight );
}

function animate() {
    if(bool_render)
    {
        requestAnimationFrame( animate );
        render();
    }
}
var rot=0;
function render() {
    if(bool_loading)
    {
        // kale.kaleShader.uniforms.resolution.value.x = 1;
        // kale.kaleShader.uniforms.resolution.value.y = 1;
        // if(0.01+beta>0.1)
        // {
        //     kale.kaleShader.uniforms.time.value+=0.1
        // }
        // else if(0.01+beta<-0.1)
        // {
        //     kale.kaleShader.uniforms.time.value+=-0.1
        // }
        // else
        // {
        //     kale.kaleShader.uniforms.time.value += (0.005+gamma);
        // }
        // kale.kaleShader.uniforms.angle.value+=beta;
    }
    if(bool_render)
    {
        renderer.render( scene, camera );
    }
    if(controls)
    {
        controls.update()
        if(gammaChange!=0)
        {
            controls.myRotateLeft(-gammaChange/1000);
            gammaChange=0;
        }
        if(betaChange!=0)
        {
            controls.myRotateUp(-betaChange/2500);
            betaChange=0;
        }

    }
    // mesh1.rotation.y+=0.001;
    // rot+=0.01;
    // controls.myRotateUp(0.001);

}

function threeOut()
{
    bool_render=false;
}
